home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample PMSAM / PMSAM Framework / Common / CRecordID.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  10.2 KB  |  487 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CRecordID.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Tim Harnett
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <7>     2/27/95    TMH        adapt to use ETO16 universal headers
  13.          <6>     2/17/95    TMH        added CRecordID(short pcatDSRefNum,StringPtr recName,StringPtr
  14.                                     recType)
  15.          <5>     2/14/95    CL        remove globals
  16.          <4>    10/18/94    TMH        added PackKeyChainRID
  17.          <3>    10/13/94    TMH        use GetDSRefNum(), big bug fix
  18.          <2>    10/11/94    TMH        added CRecordID(short pcatDSRefNum,char* recName,char* recType)
  19.          <1>     9/20/94    TMH        Abandon RoadsideRest embrace Mercury
  20.          <2>     6/14/94    TMH        added RecordID* rid assignment operator
  21.                   4/5/94    TMH        xxx put comment here xxx
  22.  
  23.     To Do:
  24. */
  25.  
  26.  
  27. #ifndef __OCE__
  28. #include "OCE.h"
  29. #endif
  30.  
  31. #ifndef __OCEAUTHDIR__
  32. #include "OCEAuthDir.h"
  33. #endif
  34.  
  35. #ifndef __Globals__
  36. #include "Globals.h"
  37. #endif
  38.  
  39.  
  40. #ifndef __CRecordID__
  41. #include "CRecordID.h"
  42. #endif
  43.  
  44. #ifndef __CAttribute__
  45. #include "CAttribute.h"
  46. #endif
  47.  
  48.  
  49.  
  50. //----------------------
  51. //    C P a c k e d R L I
  52. //----------------------
  53.  
  54.  
  55. //-------------------------
  56. //    C R e c o r d I D 
  57. //-------------------------
  58.  
  59.  
  60. //------------------------------------------------------------------------------------------
  61. CRecordID::CRecordID(short pcatDSRefNum, CreationID cid) 
  62. {
  63.     this->Initialize();
  64.     fCID = cid;
  65.     
  66.     
  67.     _fPackedRLI.fDSRefNum = pcatDSRefNum;
  68.         
  69. }
  70.  
  71. //------------------------------------------------------------------------------------------
  72. CRecordID::CRecordID(short pcatDSRefNum,char* recName,char* recType) 
  73. {
  74.     this->Initialize();    
  75.     
  76.     _fRecordName = recName;
  77.     _fRecordType = recType;
  78.     
  79.     _fPackedRLI.fDSRefNum = pcatDSRefNum;
  80.         
  81. }
  82.  
  83. //------------------------------------------------------------------------------------------
  84. CRecordID::CRecordID(short pcatDSRefNum,StringPtr recName,StringPtr recType) 
  85. {
  86.     this->Initialize();    
  87.     
  88.     _fRecordName = recName;
  89.     _fRecordType = recType;
  90.     
  91.     _fPackedRLI.fDSRefNum = pcatDSRefNum;
  92.         
  93. }
  94.  
  95.  
  96. //------------------------------------------------------------------------------------------
  97. CRecordID::CRecordID(CRecordID& cRID)
  98. {
  99.         // Caution should work with a RecordID* cast to be CRecordID&
  100.     this->Initialize();
  101.     
  102.     if( cRID.fName != 0 )
  103.         _fRecordName = *cRID.fName;
  104.         
  105.     if( cRID.fType != 0 )
  106.         _fRecordType = *cRID.fType;
  107.  
  108.     fCID = cRID.fCID;
  109.     BlockMove(cRID.fRLI,fRLI,sizeof(CPackedRLI));
  110. }
  111.  
  112.  
  113.  
  114. //------------------------------------------------------------------------------------------
  115. CRecordID::CRecordID(PackedDSSpecPtr packedDSSpec)
  116. {
  117.     extern DNodeNum MapPathNameToDNode(RLI *aRLI,short dsRefNum);        // From ADASUtils.h
  118.  
  119.     
  120.     this->Initialize();
  121.  
  122.     this->SetFromPackedDSSpec(packedDSSpec);
  123.  
  124. }
  125.  
  126.  
  127. //------------------------------------------------------------------------------------------
  128. CRecordID::CRecordID(CRLI& rli,CRecordName& recordName,CRecordType& recordType)
  129. {
  130.     this->Initialize();
  131.     
  132.     OCEPackRLI((RLI*)&rli,*fRLI,sizeof(CPackedRLI));
  133.     
  134.     _fRecordName = recordName;
  135.     _fRecordType = recordType;
  136.     
  137. }
  138.  
  139.  
  140. //------------------------------------------------------------------------------------------
  141. CRecordID::CRecordID(short dsRefNum, PackedRecordID* packedRecordID)
  142. {
  143.     this->Initialize();
  144.  
  145.     RecordID            rid;
  146.     OCEUnpackRecordID(packedRecordID,&rid);
  147.     
  148.     _fRecordName = rid.local.recordName;    // copy
  149.         
  150.     _fRecordType = rid.local.recordType;   // copy
  151.     
  152.     fCID = rid.local.cid;
  153.  
  154.     _fPackedRLI.fDSRefNum = dsRefNum;
  155.  
  156. }
  157.  
  158.  
  159.  
  160. //------------------------------------------------------------------------------------------
  161. void CRecordID::Initialize()
  162. {
  163.     memset(this,0,sizeof(CRecordID));
  164.     
  165.     fRLI = &_fPackedRLI;
  166.     fName = &_fRecordName;
  167.     fType = &_fRecordType;
  168.     
  169. }
  170.  
  171.  
  172.  
  173.  
  174. //------------------------------------------------------------------------------------------
  175. CRecordID& CRecordID::operator =(CRecordID& cRID)
  176. {
  177.     if( cRID.fName != 0 )
  178.         _fRecordName = *cRID.fName;
  179.         
  180.     if( cRID.fType != 0 )
  181.         _fRecordType = *cRID.fType;
  182.  
  183.     fCID = cRID.fCID;
  184.     BlockMove(cRID.fRLI,fRLI,sizeof(CPackedRLI));
  185.     return    *this;
  186. }
  187.  
  188.  
  189.  
  190. //------------------------------------------------------------------------------------------
  191. CRecordID& CRecordID::operator =(RecordID* rid)
  192. {
  193.     if( rid->local.recordName != 0 )
  194.         _fRecordName = rid->local.recordName;
  195.         
  196.     if( rid->local.recordType != 0 )
  197.         _fRecordType = rid->local.recordType;
  198.  
  199.     fCID = rid->local.cid;
  200.     BlockMove(rid->rli,fRLI,sizeof(CPackedRLI));
  201.     return    *this;
  202. }
  203.  
  204.  
  205. //-------------------------------------------------------------
  206. KeyChainPackedRIDPtr    CRecordID::PackKeyChainRID(KeyChainPackedRIDPtr pRID)
  207.     RecordID rid;
  208.     rid.rli = 0;
  209.     rid.local.recordName = 0;
  210.     rid.local.recordType = 0;
  211.     rid.local.cid.source = fCID.source;
  212.     rid.local.cid.seq = fCID.seq;
  213.     
  214.     OCEPackRecordID(&rid,(PackedRecordID*) pRID, kKeyChainPackedRIDSize);
  215.     
  216.     return pRID;
  217.     
  218.  }
  219.  
  220. //-------------------------------------------------------------
  221. void CRecordID::SetFromPackedDSSpec(PackedDSSpecPtr packedDSSpec)
  222. {
  223.     extern DNodeNum MapPathNameToDNode(RLI *aRLI,short dsRefNum);        // From ADASUtils.h
  224.  
  225.     RecordID            aRID;
  226.     DSSpec            dsspec;
  227.  
  228.     OCEUnpackDSSpec(packedDSSpec, &dsspec, &aRID);
  229.     
  230.     RLI            aRLI;
  231.     OCEUnpackRLI (aRID.rli, &aRLI);
  232.         
  233.     if( aRLI.dNodeNumber == 0 )     // if it is a packed pathname map it to the dNodeNumber.
  234.         MapPathNameToDNode(&aRLI,0);        // not well tested
  235.  
  236.  
  237.         //    copy the values
  238.         
  239.         
  240.     if( aRLI.dNodeNumber != 0 ) {
  241.         _fPackedRLI = &aRLI;
  242.         _fRecordName = aRID.local.recordName;
  243.         _fRecordType = aRID.local.recordType;
  244.         fCID = aRID.local.cid;
  245.     }
  246.     
  247. }
  248.  
  249.  
  250.  
  251. //-------------------------------------------------------------
  252. OSErr CRecordID::GetNameAndType()
  253. {
  254.     //    CAUTION! This may change the name of this record.
  255.     DirGetNameAndTypePB    pb;
  256.     memset(&pb, 0, sizeof(DirGetNameAndTypePB));                // Build Packed Path Name from dNodePath
  257.  
  258.     
  259.     pb.dsRefNum = this->GetDSRefNum();
  260.     pb.identity = gIdentity;
  261.  
  262.     long nilHint = 0;
  263.     pb.serverHint = *(AddrBlock*)&nilHint;
  264.  
  265.     pb.ioCompletion = nil;
  266.  
  267.     pb.aRecord = *this;
  268.     
  269.     return DirGetNameAndType((DirParamBlock*)&pb, false);
  270.  
  271. }
  272.  
  273.  
  274. //-------------------------------------------------------------
  275. OSErr CRecordID::SetNameAndType( Boolean allowDups)
  276. {
  277.     //    CAUTION! This may change the name of this record.
  278.     DirSetNameAndTypePB    pb;
  279.     
  280.     pb.dsRefNum = this->GetDSRefNum();
  281.     pb.identity = gIdentity;
  282.  
  283.     long nilHint = 0;
  284.     pb.serverHint = *(AddrBlock*)&nilHint;
  285.  
  286.     pb.ioCompletion = nil;
  287.  
  288.     pb.aRecord = *this;
  289.     pb.allowDuplicate = allowDups;
  290.     pb.newName = (RString*)&_fRecordName;
  291.     pb.newType = (RString*)&_fRecordType;
  292.     
  293.     return DirSetNameAndType((DirParamBlock*)&pb, false);
  294.  
  295. }
  296.  
  297.  
  298. //-------------------------------------------------------------
  299. OSErr CRecordID::AddAsPseudoNym(Boolean allowDuplicate)
  300. {
  301.     DirAddPseudonymPB pb;
  302.     memset(&pb,sizeof(DirAddPseudonymPB),0);
  303.     
  304.     
  305.     pb.dsRefNum = this->GetDSRefNum();
  306.     pb.identity = gIdentity;
  307.  
  308.     long nilHint = 0;
  309.     pb.serverHint = *(AddrBlock*)&nilHint;
  310.     
  311.     pb.allowDuplicate =    allowDuplicate;  // will give dup error if record or pseudonym exists.
  312.  
  313.     pb.aRecord = *this;
  314.  
  315.     pb.pseudonymName =  (RString*)&_fRecordName;
  316.     pb.pseudonymType =  (RString*)&_fRecordType;
  317.     
  318.     return DirAddPseudonym((DirParamBlockPtr)&pb, false);
  319. }
  320.  
  321.  
  322. //-------------------------------------------------------------
  323. OSErr CRecordID::DeletePseudoNym()
  324. {
  325.     DirDeletePseudonymPB pb;
  326.     memset(&pb,sizeof(DirDeletePseudonymPB),0);
  327.     
  328.     
  329.     pb.dsRefNum = this->GetDSRefNum();
  330.     pb.identity = gIdentity;
  331.     
  332.     long nilHint = 0;
  333.     pb.serverHint = *(AddrBlock*)&nilHint;
  334.     
  335.     pb.aRecord = *this;
  336.  
  337.  
  338.     //    We assume that no grief is caused by the record name.
  339.  
  340.  
  341.     pb.pseudonymName =  (RString*)&_fRecordName;
  342.     pb.pseudonymType =  (RString*)&_fRecordType;
  343.     
  344.     return DirDeletePseudonym((DirParamBlockPtr)&pb, false);
  345. }
  346.  
  347.  
  348.  
  349. //-------------------------------------------------------------
  350. OSErr CRecordID::Delete()
  351. {
  352.  
  353.     
  354.     DirDeleteRecordPB        pb;
  355.     memset(&pb,sizeof(DirDeleteRecordPB),0);
  356.     
  357.     pb.dsRefNum = this->GetDSRefNum();
  358.     pb.identity = gIdentity;
  359.     
  360.     long nilHint = 0;
  361.     pb.serverHint = *(AddrBlock*)&nilHint;
  362.  
  363.     pb.aRecord = *this;
  364.     return DirDeleteRecord((DirParamBlockPtr)&pb, false);
  365. }
  366.  
  367.  
  368.  
  369.  
  370. //-------------------------------------------------------------
  371. OSErr CRecordID::Create(Boolean allowDuplicate)
  372. {
  373.  
  374.     
  375.     DirAddRecordPB    pb;
  376.     memset(&pb,sizeof(DirAddRecordPB),0);
  377.  
  378.     pb.dsRefNum = this->GetDSRefNum();
  379.     pb.identity = gIdentity;
  380.     
  381.     long nilHint = 0;
  382.     pb.serverHint = *(AddrBlock*)&nilHint;
  383.  
  384.     pb.aRecord = *this;
  385.     
  386.     pb.allowDuplicate = allowDuplicate;
  387.  
  388.     return DirAddRecord((DirParamBlockPtr)&pb, false);
  389.     
  390.  
  391. }
  392.  
  393.     
  394. //-------------------------------------------------------------
  395. OSErr CRecordID::CreateAsAlias(CRecordID& origRID, Boolean allowDuplicate,long /*backPtrFlags*/)
  396. {
  397.  
  398.         //    Create the alias record in catalog server.
  399.         
  400.         
  401.     DirAddAliasPB    pb;
  402.     memset(&pb,sizeof(DirAddAliasPB),0);
  403.     pb.dsRefNum = this->GetDSRefNum();
  404.     pb.identity = gIdentity;
  405.     
  406.     long nilHint = 0;
  407.     pb.serverHint = *(AddrBlock*)&nilHint;
  408.  
  409.     
  410.     pb.aRecord = *this;
  411.     pb.allowDuplicate = allowDuplicate;
  412.     OSErr osErr = DirAddAlias((DirParamBlockPtr)&pb, false);
  413.     
  414.  
  415.     if( osErr == 0 ) {
  416.     
  417.         PackedDSSpec** packedDSSpec = 0;
  418.  
  419.             //    Create the alias attribute.
  420.             
  421.         packedDSSpec = (PackedDSSpec**) origRID.PackDSSpec();
  422.         CAttribute aliasAttr( kAliasAttrTypeNum, typePackedDSSpec );
  423.         aliasAttr.fDataLength =(*packedDSSpec)->dataLength + sizeof(short);
  424.         aliasAttr.fBytes = (void *)*packedDSSpec;
  425.         
  426.         osErr =  aliasAttr.AddAttribute( *this ) ;
  427.  
  428. #ifdef ALIASBACKPOINTERS        
  429.         if( osErr == 0 ) {
  430.         
  431.         
  432.             //    Alias Backpointer
  433.             
  434.             
  435.             AliasBackpointer bp;
  436.             bp.cid = fCID;
  437.             bp.node = this->Node();
  438.             bp.flags = backPtrFlags;
  439.             CAttribute backpointerAttr( kAliasBackPointerTypeNum, typeBinary );
  440.             backpointerAttr.fDataLength = sizeof(AliasBackpointer);
  441.             backpointerAttr.fBytes = (void *)&bp;
  442.             osErr = backpointerAttr.AddAttribute( origRID );
  443.             
  444.             
  445.         }
  446.                     
  447. #endif    
  448.         if( osErr != 0 )
  449.             this->Delete();
  450.  
  451.         if( packedDSSpec != 0 )
  452.             DisposeHandle((Handle)packedDSSpec);
  453.  
  454.  
  455.     }
  456.     
  457.     return osErr;
  458.     
  459. }
  460.  
  461.  
  462.  
  463. //-------------------------------------------------------------
  464. PackedDSSpec** CRecordID::PackDSSpec()
  465. {
  466.  
  467.         // Caller is responsible for Disposing result.
  468.         
  469.         
  470.     DSSpec            aliasDSSpec;
  471.     aliasDSSpec.entitySpecifier    =    *this;
  472.     aliasDSSpec.extensionType        =    kOCEentnDSSpec;
  473.     aliasDSSpec.extensionSize        =    0;
  474.     aliasDSSpec.extensionValue        =    0;
  475.  
  476.     unsigned short packedDSSpecLen = OCEPackedDSSpecSize (&aliasDSSpec);
  477.     PackedDSSpec** packedDSSpec = (PackedDSSpec**) NewHandle(packedDSSpecLen);
  478.     
  479.     OCEPackDSSpec (&aliasDSSpec, *packedDSSpec, packedDSSpecLen);
  480.     
  481.     return packedDSSpec;
  482. }
  483.  
  484.  
  485.  
  486.